﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using QuanLyBanHang.DAL;
using System.Windows.Forms;

namespace QuanLyBanHang
{
    public partial class Form_KH : Form
    {
        KhachHang_DAL khachhang = new KhachHang_DAL();
        bool them = false;
        bool sua = false;
        bool xoa = false;
        public Form_KH()
        {
            InitializeComponent();
        }
        public void xemKH()
        {
            
            dataGridView1.DataSource = khachhang.laykhachhang();


            //dataGridView1.Rows.Add(dataGridView1.Rows.)

        }

        private void Form_KH_Load(object sender, EventArgs e)
        {
            xemKH();
        }

        private void timkiemBut_Click(object sender, EventArgs e)
        {
            dataGridView1.DataSource = khachhang.timkiemkh(timtenkhTxt.Text);
            timtenkhTxt.Text = "";
        }

        private void themBut_Click(object sender, EventArgs e)
        {

            panel1.Enabled = true;
            panel2.Enabled = false;
            panel3.Enabled = false;
            luuBut.Enabled = true;
            huyBut.Enabled = true;
            them = true;
            MessageBox.Show("Nhập thông tin!");
        }

        private void luuBut_Click(object sender, EventArgs e)
        {
            if (them == true)
            {
                if (khachhang.themkh(hotenkhTxt.Text, diachiTxt.Text, emailTxt.Text, sodienthoaiTxt.Text) == true) MessageBox.Show("Thêm thành công!");
                else
                    MessageBox.Show("Có lỗi!");
                xemKH();
                huyBut_Click(sender, e);

            }
            if (sua == true)
            {
                if (khachhang.suakhachhang(int.Parse(dataGridView1.CurrentRow.Cells[0].Value.ToString()), hotenkhTxt.Text,diachiTxt.Text,emailTxt.Text,sodienthoaiTxt.Text) == true)
                    MessageBox.Show("Sửa thành công!");
                else
                    MessageBox.Show("Có lỗi!");
                xemKH();
                huyBut_Click(sender, e);
            }

            if (xoa == true)
            {
                if (khachhang.xoakhachhang(int.Parse(dataGridView1.CurrentRow.Cells[0].Value.ToString())) == true)
                    MessageBox.Show("Xoa thanh cong!");
                else
                    MessageBox.Show("Co loi!");
                xemKH();
                huyBut_Click(sender, e);
            }

            if (xoa == true)
            {
                if (khachhang.xoakhachhang(int.Parse(dataGridView1.CurrentRow.Cells[0].Value.ToString())) == true)
                    MessageBox.Show("Xóa thành công!");
                else
                    MessageBox.Show("Có lỗi!");
                xemKH();
                huyBut_Click(sender, e);
            }

        }

        private void huyBut_Click(object sender, EventArgs e)
        {
            them = false;
            sua = false;
            xoa = false;
            panel1.Enabled = false;
            hotenkhTxt.Text = "";
            diachiTxt.Text = "";
            emailTxt.Text = "";
            sodienthoaiTxt.Text = "";
            panel2.Enabled = true;
            panel3.Enabled = true;
            luuBut.Enabled = false;
            huyBut.Enabled = false;

        }

        private void suaBut_Click(object sender, EventArgs e)
        {
            sua = true;
            panel1.Enabled = true;
            huyBut.Enabled = true;
            luuBut.Enabled = true;           
            panel3.Enabled = false;
            MessageBox.Show("Chọn khách hàng cần sửa!");




        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {

           
          
        }

        private void thoatBut_Click(object sender, EventArgs e)
        {
            Dispose();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (sua == true||xoa==true)
            {
                hotenkhTxt.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
                diachiTxt.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
                emailTxt.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
                sodienthoaiTxt.Text = dataGridView1.CurrentRow.Cells[4].Value.ToString();
            }
        }

      


        private void xoaBut_Click(object sender, EventArgs e)
        {
            xoa = true;
            panel1.Enabled = false;
            huyBut.Enabled = true;
            luuBut.Enabled = true;
            panel3.Enabled = false;
            MessageBox.Show("Chọn khách hàng cần xoa!");

        }


       

      



        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void sodienthoaiTxt_KeyPress(object sender, KeyPressEventArgs e)
        {
            if((!char.IsDigit(e.KeyChar))&&(!char.IsControl(e.KeyChar))) e.Handled = true;
        }

    }
}
